# -*- coding:utf-8 -*-
"""
添加定时计划
"""
import os


def int2str(number):
    
    if number<10:
        return '0{}'.format(number)
    else:
        return str(number)
    
    


def createSchduleEveryDay(task_name:str,cmd:str,start_hour:int,start_minute:int,end_hour=None,end_minute=None):
    """
    计划每天指定时间执行任务
    :param task_name: 任务名称
    :param cmd:   执行cmd命令 eg : "C:/Python37/pythonw.exe D:/Test/test.py"  如果pythonw.exe在环境变量当中，可以直接
                "pythonw.exe D:/Test/test.py" 
    :param start_hour: 指定小时
    :param start_minute: 指定分钟
    :param end_hour: 强制结束执行具体小时
    :param end_minut: 强制结束具体分钟

    :return: 
    """

    schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SC DAILY /ST {}:{} '.\
        format(task_name,cmd,int2str(start_hour),int2str(start_minute))
    if end_hour and end_minute:
        schdule = 'SCHTASKS /Create /TN {} /TR  "{}"   /SC DAILY /ST {}:{} /ET {}:{}'.\
            format(task_name,cmd,int2str(start_hour),int2str(start_minute),int2str(end_hour),int2str(end_minute))

    os.system(schdule)
    
    

def createSchduleIntervalDays(task_name:str,cmd:str,days:int,start_hour:int,start_minute:int,end_hour=None,end_minute=None):
    """
    每隔多少天执行一次
    :param task_name: 任务名称
    :param cmd:   执行cmd命令 eg : "C:/Python37/pythonw.exe D:/Test/test.py"  如果pythonw.exe在环境变量当中，可以直接
            "pythonw.exe D:/Test/test.py"
    :param days:  间隔天数
    :param start_hour:  执行具体小时
    :param start_minute: 执行具体分钟
    :param end_hour:  强制结束执行具体小时
    :param end_minute: 强制结束具体分钟
    :return:
    """
    schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SC DAILY /MO {} /ST {}:{} '.format(task_name, cmd,days,
                                                                                            int2str(start_hour),
                                                                                            int2str(start_minute))
    if end_hour and end_minute:
        schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SC DAILY /MO {} /ST {}:{} /ET {}:{} '.\
            format(task_name, cmd,days,int2str(start_hour),int2str(start_minute),int2str(end_hour),int2str(end_minute))

    os.system(schdule)


def createSchduleIntervalWeeks(task_name:str,cmd:str,weeks:int,start_hour:int,start_minute:int,end_hour=None,end_minute=None):
    """
    每隔多少天执行一次
    :param task_name: 任务名称
    :param cmd:   执行cmd命令 eg : "C:/Python37/pythonw.exe D:/Test/test.py"  如果pythonw.exe在环境变量当中，可以直接
            "pythonw.exe D:/Test/test.py"
    :param weeks:  间隔周数
    :param start_hour:  执行具体小时
    :param start_minute: 执行具体分钟
    :param end_hour:  强制结束执行具体小时
    :param end_minute: 强制结束具体分钟
    :return:
    """
    if weeks > 0 and weeks< 53:
        pass
    else:
        raise RuntimeError('weeks must in [0,52]')
    schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SC WEEKLY /MO {} /ST {}:{} '.format(task_name, cmd,weeks,
                                                                                            int2str(start_hour),
                                                                                            int2str(start_minute))
    if end_hour and end_minute:
        schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SC WEEKLY /MO {} /ST {}:{} /ET {}:{} '.\
            format(task_name, cmd,weeks,int2str(start_hour),int2str(start_minute),int2str(end_hour),int2str(end_minute))

    os.system(schdule)


def createSchduleEveryWeek(task_name:str,cmd:str,start_hour:int,start_minute:int,end_hour=None,end_minute=None):
    """
    每1周天执行一次
    :param task_name: 任务名称
    :param cmd:   执行cmd命令 eg : "C:/Python37/pythonw.exe D:/Test/test.py"  如果pythonw.exe在环境变量当中，可以直接
            "pythonw.exe D:/Test/test.py"
    :param start_hour:  执行具体小时
    :param start_minute: 执行具体分钟
    :param end_hour:  强制结束执行具体小时
    :param end_minute: 强制结束具体分钟
    :return:
    :return:
    """
    schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SC WEEKLY  /ST {}:{} '.format(task_name, cmd,
                                                                                            int2str(start_hour),
                                                                                            int2str(start_minute))
    if end_hour and end_minute:
        schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SC WEEKLY  /ST {}:{} /ET {}:{} '.\
            format(task_name, cmd,int2str(start_hour),int2str(start_minute),int2str(end_hour),int2str(end_minute))

    os.system(schdule)




def createSchduleEveryWeekday(task_name:str,cmd:str,day_number:int,start_hour:int,start_minute:int,end_hour=None,end_minute=None):
    """
    每周几执行
    :param task_name: 任务名称
    :param cmd:   执行cmd命令 eg : "C:/Python37/pythonw.exe D:/Test/test.py"  如果pythonw.exe在环境变量当中，可以直接
            "pythonw.exe D:/Test/test.py"
    :param day_number:  每周几, 星期一:1 ,星期二:2,...星期天:7
    :param start_hour:  执行具体小时
    :param start_minute: 执行具体分钟
    :param end_hour:  强制结束执行具体小时
    :param end_minute: 强制结束具体分钟
    :return:
    """
    if day_number > 0 and day_number< 8:
        pass
    else:
        raise RuntimeError('day_number must in [1,7]')

    weekday_map = {1:'MON',2:'TUE',3:'WED',4:'THU',5:'FRI',6:'SAT',7:'SUN'}
    schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SC WEEKLY /D {} /ST {}:{} '.format(task_name, cmd,weekday_map[day_number],
                                                                                            int2str(start_hour),
                                                                                            int2str(start_minute))
    if end_hour and end_minute:
        schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SC WEEKLY /D {} /ST {}:{} /ET {}:{} '.\
            format(task_name, cmd,weekday_map[day_number],int2str(start_hour),int2str(start_minute),int2str(end_hour),int2str(end_minute))

    os.system(schdule)




def createSchduleEveryMonth(task_name:str,cmd:str,start_hour:int,start_minute:int,end_hour=None,end_minute=None):
    """
    每月执行1次
    :param task_name: 任务名称
    :param cmd:   执行cmd命令 eg : "C:/Python37/pythonw.exe D:/Test/test.py"  如果pythonw.exe在环境变量当中，可以直接
            "pythonw.exe D:/Test/test.py"
    :param start_hour:  执行具体小时
    :param start_minute: 执行具体分钟
    :param end_hour:  强制结束执行具体小时
    :param end_minute: 强制结束具体分钟
    :return:
    """
    schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SC MONTHLY /ST {}:{} '.format(task_name, cmd,
                                                                                            int2str(start_hour),
                                                                                            int2str(start_minute))
    if end_hour and end_minute:
        schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SC MONTHLY  /ST {}:{} /ET {}:{} '.\
            format(task_name, cmd,int2str(start_hour),int2str(start_minute),int2str(end_hour),int2str(end_minute))

    os.system(schdule)


def createSchduleIntervalMonths(task_name:str,cmd:str,months:int,start_hour:int,start_minute:int,end_hour=None,end_minute=None):
    """
    每隔几个月执行一次
    :param task_name: 任务名称
    :param cmd:   执行cmd命令 eg : "C:/Python37/pythonw.exe D:/Test/test.py"  如果pythonw.exe在环境变量当中，可以直接
            "pythonw.exe D:/Test/test.py"
    :param months:  所隔月数
    :param start_hour:  执行具体小时
    :param start_minute: 执行具体分钟
    :param end_hour:  强制结束执行具体小时
    :param end_minute: 强制结束具体分钟
    :return:
    """
    if months > 0 and months< 13:
        pass
    else:
        raise RuntimeError('months must in [1,12]')

    schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SC MONTHLY /MO {} /ST {}:{} '.format(task_name, cmd,months,
                                                                                            int2str(start_hour),
                                                                                            int2str(start_minute))
    if end_hour and end_minute:
        schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SCMONTHLY /MO  {} /ST {}:{} /ET {}:{} '.\
            format(task_name, cmd,months,int2str(start_hour),int2str(start_minute),int2str(end_hour),int2str(end_minute))

    os.system(schdule)




def createSchduleIntervalTime(task_name:str,cmd:str,hours,minutes,start_hour:int,start_minute:int,end_hour=None,end_minute=None):
    """
    每隔指定小时或分钟执行
    :param task_name:
    :param cmd:
    :param hour:
    :param minute:
    :param end_hour:
    :param end_minute:
    :return:
    """
    all_minutes = hours*60 + minutes
    schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SC MINUTE /MO {} /ST {}:{} '.format(task_name, cmd,all_minutes,
                                                                                                   int2str(start_hour),
                                                                                                   int2str(start_minute))
    if end_hour and end_minute:
        schdule = 'SCHTASKS /Create /TN {} /TR  "{}"  /SC MINUTE /MO {} /ST {}:{} /ET {}:{} '.format(task_name, cmd, all_minutes,
                                                                                           int2str(start_hour),
                                                                                           int2str(start_minute),
                                                                                           int2str(end_hour),
                                                                                           int2str(end_minute))
    os.system(schdule)
    
def deleteSchedule(task_name):
    """
    删除任务
    :param task_name: 任务名称
    :return:
    """
    delete = "schtasks /delete /TN {} /F".format(task_name)
    os.system(delete)





def findSchedule(task_name):
    """
    查询任务
    :param task_name:
    :return:
    """
    find_cmd = "schtasks /query /TN {}".format(task_name)

    result = os.popen(find_cmd)

    text = result.read()
    if task_name in text:
        return True
    else:
        return False


















